(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:rolelist
     * @description
     * # rolelist 
     */
    angular.module('webApp')
        //调用其他方法
        .run(["$templateCache", "dataService", function($templateCache, dataService) {
            var tplName = "views/directives.rolelist.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }])
        .directive('rolelist', ['dataService', '$compile', function(dataService, $compile) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '='
                },
                template: "<a href='javascript:void(0);' class='sui-btn btn-success' ng-click='chose();'><i class='sui-icon icon-tb-add'></i>{{'choice' | translate}}</a>",
                controller: ['$templateCache', '$rootScope', '$scope', '$timeout', function($templateCache, $rootScope, $scope, $timeout) {
                    $scope.items = [];
                    $scope.chose = function() {
                        $scope.pageData().then(showPop);
                    };
                    $scope.setRandKey = function(key) {
                        $scope.randKey = key;
                        console.log("randKey:", $scope.randKey);
                    }

                    function showPop() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '<form class="sui-form" style="margin:0px;"><span>{{"Roles" | translate}}</span></form>',
                            body: $templateCache.get("views/directives.rolelist.html").replace("random-key", $scope.randKey),
                            okBtn: '{{"ok"| translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'small',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    // console.log("选择的数据：", $scope.ngModel);
                                    // $scope.setValue();
                                });
                            },
                        });
                    }

                    //请求数据
                    $scope.itemsTotal = 0;
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        var query = { page_no: pageNo, page_size: $scope.pageSize };
                        if ($scope.keyword) {
                            query.query = { "title": $scope.keyword };
                        }

                        return dataService.roleGet(query).then(function(data) {
                            $scope.items = data.roles;
                            $scope.itemsTotal = data.total_count;
                        });
                    }
                }],
                link: function(scope, element, attrs, ctrl) {
                    scope.$watch('ngModel', function(newValue, oldValue, scope) {
                        if (_.isObject(newValue)) {
                            ctrl.$setViewValue(newValue);
                        }
                    });

                    //初始化，获取数据
                    ctrl.$render = function() {
                        if (_.isNumber(ctrl.$viewValue)) {
                            dataService.roleOne(ctrl.$viewValue).then(function(a) {
                                ctrl.$setViewValue(a);
                            })
                        }
                    };
                }
            }
        }])
        .directive('roleone', ['dataService', function(dataService) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '='
                },
                template: "<span>{{ngModel.name}}</span>",
                link: function(scope, element, attrs, ctrl) {
                    //初始化，获取数据
                    ctrl.$render = function() {
                        if (_.isNumber(ctrl.$viewValue)) {
                            dataService.roleOne(ctrl.$viewValue).then(function(a) {
                                ctrl.$setViewValue(a);
                            })
                        }
                    };
                }
            }
        }]);
})();
